﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SCMS.Model;
using System.Data.Objects.DataClasses;

namespace SCMS.CoreBusinessLogic.FleetManager
{
    public class FleetDetailsService : IFleetDetailsService
    {
        public bool IsFleetDetailsSaved(FleetDetail Entity)
        {
            using (var db = new SCMSEntities())
            {
                db.FleetDetails.AddObject(Entity);
                return db.SaveChanges() > 0 ? true : false;
            }
        }

        public bool IsDailyFleetSaved(FleetDailyManagement Entity)
        {
            using (var db = new SCMSEntities())
            {
                db.FleetDailyManagements.AddObject(Entity);
                return db.SaveChanges() > 0 ? true : false;
            }
        }

        public bool IsGarageInfoSaved(GarageInfo Entity)
        {
            using (var db = new SCMSEntities())
            {
                db.GarageInfoes.AddObject(Entity);
                return db.SaveChanges() > 0 ? true : false;
            }
        }

        public bool IsDriverInfoSaved(DriverDetail Entity)
        {
            using (var db = new SCMSEntities())
            {
                db.DriverDetails.AddObject(Entity);
                return db.SaveChanges() > 0 ? true : false;
            }
        }

        public bool IsAccesoryAdded(Equipment2Fleet Entity)
        {
            using (var db = new SCMSEntities())
            {
                db.Equipment2Fleet.AddObject(Entity);
                return db.SaveChanges() > 0 ? true : false;
            }
        }

        public bool IsFleetDetailsEdited(FleetDetail entity)
        {
            using (var db = new SCMSEntities())
            {
                db.FleetDetails.Attach(entity);
                db.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified);
                return db.SaveChanges() > 0 ? true : false;
            }
        }

        public bool IsDriverInfoEdited(DriverDetail entity)
        {
            using (var db = new SCMSEntities())
            {
                db.DriverDetails.Attach(entity);
                db.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified);
                return db.SaveChanges() > 0 ? true : false;
            }
        }

        public bool IsGarageInfoEdited(GarageInfo entity)
        {
            using (var db = new SCMSEntities())
            {
                db.GarageInfoes.Attach(entity);
                db.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified);
                return db.SaveChanges() > 0 ? true : false;
            }
        }

        public bool IsDailyFleetEdited(FleetDailyManagement entity)
        {
            using (var db = new SCMSEntities())
            {
                db.FleetDailyManagements.Attach(entity);
                db.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified);
                return db.SaveChanges() > 0 ? true : false;
            }
        }

        public bool IsFleetDeleted(Guid fleetId)
        {
            using (var db = new SCMSEntities())
            {
                db.FleetDetails.DeleteObject(db.FleetDetails.FirstOrDefault(p => p.Id == fleetId));
                return db.SaveChanges() > 0 ? true : false;
            }
        }

        public bool IsDriverInfoDeleted(Guid driverInfoId)
        {
            using (var db = new SCMSEntities())
            {
                db.DriverDetails.DeleteObject(db.DriverDetails.FirstOrDefault(p => p.Id == driverInfoId));
                return db.SaveChanges() > 0 ? true : false;
            }
        }

        public bool IsGarageInfoDeleted(Guid garageInfoId)
        {
            using (var db = new SCMSEntities())
            {
                db.GarageInfoes.DeleteObject(db.GarageInfoes.FirstOrDefault(p => p.Id == garageInfoId));
                return db.SaveChanges() > 0 ? true : false;
            }
        }

        public bool IsDailyFleetInfoDeleted(Guid dailyFleetInfoId)
        {
            using (var db = new SCMSEntities())
            {
                db.FleetDailyManagements.DeleteObject(db.FleetDailyManagements.FirstOrDefault(p => p.Id == dailyFleetInfoId));
                return db.SaveChanges() > 0 ? true : false;
            }
        }

        public bool IsAccesoryRemoved(Guid accesoryId)
        {
            using (var db = new SCMSEntities())
            {
                db.Equipment2Fleet.DeleteObject(db.Equipment2Fleet.FirstOrDefault(p => p.Id == accesoryId));
                return db.SaveChanges() > 0 ? true : false;
            }
        }

        public List<Model.FleetDetail> GetFleetDetails(Guid cp)
        {
            using (var db = new SCMSEntities())
            {
                List<Model.FleetDetail> FDetails = db.FleetDetails.Where(p => p.CountryProgrammeId == cp).OrderByDescending(p=>p.IssueDate).ToList();
                foreach (var item in FDetails)
                {
                    Asset ast = item.Asset;
                    Model.VehicleModel vm = item.VehicleModel;
                    if (vm != null) { VehicleMake vmk = vm.VehicleMake; }
                    Model.InsuranceType insure = item.InsuranceType;
                }
                return FDetails;
            }
        }

        public float GetAverageFuelConsumed(Guid fleetId)
        {
            using (var db = new SCMSEntities())
            {
                float totalFuel = 0, totalDistance = 0, monthtotalDistance = 0;
                if (db.FleetDailyManagements.Where(p => p.FleetId == fleetId).Count() > 0)
                    totalDistance = (float)db.FleetDailyManagements.Where(p => p.FleetId == fleetId).Sum(p => p.EndMilleage);
                if (db.FleetDailyManagements.Where(p => p.FleetId == fleetId && p.Date.Value.Month == DateTime.Now.Month).Count() > 0)
                {
                    totalFuel = (float)db.FleetDailyManagements.Where(p => p.FleetId == fleetId && p.Date.Value.Month == DateTime.Now.Month).Sum(p => p.TotalFuelConsumed);
                    monthtotalDistance = (float)db.FleetDailyManagements.Where(p => p.FleetId == fleetId && p.Date.Value.Month == DateTime.Now.Month).Sum(p => p.EndMilleage);
                    return (float)Math.Round((totalDistance - monthtotalDistance) / totalFuel, 2);
                }
                else return 0;
            }
        }

        public Model.FleetDetail GetGenFleetDetailsById(Guid fleetId)
        {
            using (var db = new SCMSEntities())
            {
                Model.FleetDetail FDetails = db.FleetDetails.FirstOrDefault(p => p.Id == fleetId);
                if (FDetails != null)
                {
                    Model.VehicleModel vm = FDetails.VehicleModel;
                    if (vm != null) { Model.VehicleMake vmk = vm.VehicleMake; }
                }
                return FDetails;
            }
        }

        public Model.DriverDetail GetDriverInfoById(Guid driverInfoId)
        {
            using (var db = new SCMSEntities())
            {
                return db.DriverDetails.FirstOrDefault(p => p.Id == driverInfoId);
            }
        }

        public Model.GarageInfo GetGarageInfoById(Guid garageInfoId)
        {
            using (var db = new SCMSEntities())
            {
                return db.GarageInfoes.FirstOrDefault(p => p.Id == garageInfoId);
            }
        }

        public Model.FleetDailyManagement GetDailyFleetById(Guid dailyFleetId)
        {
            using (var db = new SCMSEntities())
            {
                return db.FleetDailyManagements.FirstOrDefault(p => p.Id == dailyFleetId);
            }
        }

        public Model.FleetDailyManagement GetLastDailyFleetRecord(Guid fleetId)
        {
            using (var db = new SCMSEntities())
            {
                return db.FleetDailyManagements.Where(p => p.FleetId == fleetId).OrderByDescending(p => p.IssueDate).FirstOrDefault();
            }
        }

        public Model.FleetDailyManagement GetLastDailyFleetRecord(FleetDailyManagement entity)
        {
            using (var db = new SCMSEntities())
            {
                return db.FleetDailyManagements.Where(p => p.IssueDate < entity.IssueDate).OrderByDescending(p => p.IssueDate).FirstOrDefault();
            }
        }

        public List<VehicleMake> GetVehicleMake(Guid cpId)
        {
            using (var db = new SCMSEntities())
            {
                return db.VehicleMakes.Where(p => p.CountryProgrammeId == cpId).ToList();
            }
        }

        public List<Model.InsuranceType> GetInsuranceTypes(Guid cpId)
        {
            using (var db = new SCMSEntities())
            {
                return db.InsuranceTypes.Where(p => p.CountryProgrammeId == cpId).ToList();
            }
        }

        public List<Model.DutyType> GetDutyTypes(Guid cpId)
        {
            using (var db = new SCMSEntities())
            {
                return db.DutyTypes.Where(p => p.CountryProgramId == cpId).ToList();
            }
        }

        public List<Model.FleetMajorIncidence> GetFleetIncidences(Guid cpId)
        {
            using (var db = new SCMSEntities())
            {
                return db.FleetMajorIncidences.Where(p => p.CountryProgramId == cpId).ToList();
            }
        }

        public List<Model.VehicleModel> GetVehicleModel(Guid mkId)
        {
            using (var db = new SCMSEntities())
            {
                return db.VehicleModels.Where(p => p.MakeId == mkId).ToList();
            }
        }

        public List<FleetEquipment> GetFleetAccesories(Guid cpId)
        {
            using (var db = new SCMSEntities())
            {
                return db.FleetEquipments.Where(p => p.CountryProgrammeId == cpId).ToList();
            }
        }
    }
}
